﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.Model;
using Wsitech.BLL;
using Wsitech.Utility;
using System.Collections;

public partial class Backend_ReportList : BackendPageBase
{
    protected readonly string sitemapid = HttpContext.Current.Request["sitemapId"] ?? "";
    protected readonly bool isEnglish = (HttpContext.Current.Request["lang"] ?? "") == "en";
    /// <summary>
    /// 栏目名称
    /// </summary>
    public string SiteMapName
    {
        get
        {
            if (!string.IsNullOrEmpty(sitemapid))
            {
                SiteMapInfo map = SiteMapInfoBLL.Instance.GetDetail(sitemapid);
                if (map != null)
                {
                    return map.SitemapName;
                }
                else
                {
                    return "找不到栏目";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCompany();
            bindReport();
        }
    }

    //绑定公司 
    private void bindCompany()
    {
        ddlCompany.DataSource = CompanyInfoBLL.Instance.GetList();
        ddlCompany.DataValueField = "CompanyId";
        if (isEnglish)
        {
            ddlCompany.DataTextField = "EnCompanyName";
        }
        else
        {
            ddlCompany.DataTextField = "CompanyName";
        }
        ddlCompany.DataBind();

        ddlCompany.Items.Insert(0, new ListItem("==全部==", ""));
    }

    //绑定信息
    private void bindReport()
    {
        Hashtable ht = new Hashtable();
        //按公司
        if (Request["companyId"] != null)
        {
            ht.Add("companyId", Request["companyId"]);
        }
        //按标题模糊
        if (Request["title"] != null)
        {
            ht.Add("title", Request["title"]);
        }
        //媒体发布起始时间
        if (Request["mediaPublishFromDate"] != null)
        {
            try
            {
                ht.Add("mediaPublishFromDate", DateTime.Parse(Request["mediaPublishFromDate"]));
            }
            catch (Exception)
            {
            }
        }
        //媒体发布截止时间
        if (Request["mediaPublishToDate"] != null)
        {
            try
            {
                ht.Add("mediaPublishToDate", DateTime.Parse(Request["mediaPublishToDate"]).AddDays(1));
            }
            catch (Exception)
            {
            }
        }
        int count = 0;

        repMain.DataSource = TextInfoBLL.Instance.GetPagedListBySiteMapID(sitemapid, ht, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref count);
        repMain.DataBind();

        AspNetPager1.RecordCount = count;
    }


    protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton lbtnEnable = (LinkButton)e.Item.FindControl("lbtnEnable");
        LinkButton lbtnDisable = (LinkButton)e.Item.FindControl("lbtnDisable");

        //显示和隐藏启用与禁用按钮
        TextInfo info = e.Item.DataItem as TextInfo;
        if (info != null)
        {
            if (info.State == (int)UtilityEnum.State.Enable)
            {
                lbtnEnable.Visible = false;
            }
            if (info.State == (int)UtilityEnum.State.Disable)
            {
                lbtnDisable.Visible = false;
            }
        }
    }
    protected void repMain_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string tid = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "Enable":
                TextInfoBLL.Instance.ChangeState(tid, UtilityEnum.State.Enable);
                break;
            case "Disable":
                TextInfoBLL.Instance.ChangeState(tid, UtilityEnum.State.Disable);
                break;
            default:
                break;
        }
        Response.Redirect(Request.Url.ToString());
    }

    //搜索
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string searchUrl = Request.Path + "?sitemapid=" + sitemapid;
        //按公司
        if (ddlCompany.SelectedIndex != 0)
        {
            searchUrl += "&companyId=" + ddlCompany.SelectedValue;
        }
        //按标题模糊
        if (txtTitle.Text.Trim() != "")
        {
            searchUrl += "&title=" + txtTitle.Text.Trim();
        }
        //媒体发布起始时间
        if (txtFrom.Text.Trim() != "")
        {
            searchUrl += "&mediaPublishFromDate=" + txtFrom.Text.Trim();
        }
        //媒体发布截止时间
        if (txtTo.Text.Trim() != "")
        {
            searchUrl += "&mediaPublishToDate=" + txtTo.Text.Trim();
        }

        Response.Redirect(searchUrl);
    }

    //分页
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindReport();
    }
}